﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ProjectCore.Application;
using ProjectCore.Common;
using ProjectCore.Domain.Model.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ProjectCore.WebApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class RolesController : ControllerBase
    {
        private readonly IAppRoles appRoles;
        public RolesController(IAppRoles _appRoles)
        {
            appRoles = _appRoles;
        }

        /// <summary>
        /// 获取角色表集合
        /// </summary>
        /// <returns></returns>

        [HttpGet]
        public async Task<ActionResult> GetRoless(int pageIndex, int pageSize)
        {
            Task<HeaderResult<List<Roles>>> roles = appRoles.GetRoles(pageIndex, pageSize);
            return Ok(await roles);
        }


        /// <summary>
        /// 添加角色
        /// </summary>
        /// 
        /// <param name="roles"></param>
        /// <returns></returns>

        [HttpPost]
        public async Task<ActionResult> AddRoles(Roles roles)
        {
            Task<int> n = appRoles.AddRoles(roles);
            return Ok(new { data = await n });
        }


        /// <summary>
        /// 反填角色数据
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        
        [HttpGet]
        public async Task<ActionResult> GetRole(int Id)
        {
            Task<Roles> roles = appRoles.GetRole(Id);
            return Ok(new
            {
                data = await roles
            });
        }


        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        
        [HttpGet]
        public async Task<ActionResult> IsDeleted(int Id)
        {
            Task<int> n = appRoles.IsDel(Id);
            return Ok(new
            {
                data = await n
            });
        }


        /// <summary>
        /// 修改角色表
        /// </summary>
        /// <param name="roles"></param>
        /// <returns></returns>
        
        [HttpPut]
        public async Task<ActionResult> UptRoles(Roles roles)
        {
            Task<int> n = appRoles.UptRoles(roles);
            return Ok(new
            {
                data = await n
            });
        }


        /// <summary>
        /// 获取角色权限列表
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet]

        public async Task<ActionResult> GetRoleMenus(int Id)
        {
            List<RoleMenus> roleMenus = await appRoles.GetRoleMenus(Id);
            return Ok(roleMenus);
        }


        /// <summary>
        /// 修改角色权限列表
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet]

        public async Task<ActionResult> UptRoleMenus(List<int> li,int Id)
        {
            int n = await appRoles.UptRoleMenu(li, Id);
            return Ok(n);
        }
    }
}
